package com.Service;

import com.Dao.OrderEngineerDao;
import com.Dao.OrdersDao;
import com.POJO.OrderEngineer;
import com.POJO.OrderEngineerInformation;
import com.POJO.Orders;
import com.Service.Imp.OrdersServiceImp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Service
public class OrdersService extends ServiceImpl<OrdersDao, Orders> implements OrdersServiceImp {
    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private OrderEngineerDao orderEngineerDao;

    public int UserOrdersAdd(String user_tel, String subject1, String subject2, String subject3, String textarea,String province,String city,String area,String addressIp,String longitude,String latitude,MultipartFile[] images){
        Orders orders=new Orders();
        LocalDateTime now = LocalDateTime.now(); // 获取当前日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String formattedDateTime = now.format(formatter); // 使用formatter将now格式化为字符串
        String uuid=UUID.randomUUID().toString()+formattedDateTime;
        orders.setOrderUuid(uuid);
        orders.setUserTel(user_tel);
        orders.setSubject1(subject1);
        orders.setSubject2(subject2);
        orders.setSubject3(subject3);
        orders.setTextarea(textarea);
        orders.setProvince(province);
        orders.setCity(city);
        orders.setArea(area);
        orders.setAddressIp(addressIp);
        BigDecimal longitude1=new BigDecimal(longitude);
        orders.setLongitude(longitude1);
        BigDecimal latitude1=new BigDecimal(latitude);
        orders.setLatitude(latitude1);
        orders.setState("0");
        int n=0;
        for(MultipartFile image:images){
            //原始文件名
            String originalFilename = image.getOriginalFilename();//xxx.png
            System.out.println(originalFilename);
            assert originalFilename != null;
            //对原始名进行截取"."后面的字符
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));//.png
            //使用UUID重新生成文件名，防止文件名称重复造成文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            File basePath = new File("src/main/resources/static/UserImages");
            // 不存在，则创建该文件夹
//            if(!basePath.exists()){basePath.mkdir();}
            String Path = basePath.getAbsolutePath() + File.separator + fileName;
            try {
                File file=new File(Path);
                //将临时文件转存到指定位置
                image.transferTo(file);
                n=n+1;
                if(n==1) orders.setImage1(fileName);
                else if(n==2) orders.setImage2(fileName);
                    else if(n==3)orders.setImage3(fileName);
                        else if(n==4)orders.setImage4(fileName);
                            else if(n==5)orders.setImage5(fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        int i=ordersDao.insert(orders);
        ordersDao.insertOrdersWithEngineerDistance(uuid,longitude1,latitude1);
        return i;
    }
    public List<Orders> findUserOrders0(String tel){
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_tel",tel);
        queryWrapper.eq("state",0);
        return ordersDao.selectList(queryWrapper);
    }
    public List<Orders> findUserOrders1(String tel){
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_tel",tel);
        queryWrapper.eq("state",1);
        return ordersDao.selectList(queryWrapper);
    }
    public List<Orders> findUserOrders2(String tel){
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_tel",tel);
        queryWrapper.eq("state",2);
        return ordersDao.selectList(queryWrapper);
    }
    public List<Orders> findUserOrders3(String tel){
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_tel",tel);
        queryWrapper.eq("state",3);
        return ordersDao.selectList(queryWrapper);
    }
    public List<Orders> findUserOrders4(String tel){
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_tel",tel);
        queryWrapper.eq("state",4);
        return ordersDao.selectList(queryWrapper);
    }
    public int UserDelOrder(String orderUuid){
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_uuid",orderUuid);
        QueryWrapper<OrderEngineer> orderEngineerQueryWrapper=new QueryWrapper<>();
        orderEngineerQueryWrapper.eq("order_uuid",orderUuid);
        orderEngineerDao.delete(orderEngineerQueryWrapper);
        return ordersDao.delete(queryWrapper);
    }
    public List<Orders> findEngineerOrders0(String tel){
        return ordersDao.findEngineerOrders0(tel);
    }

    public List<Orders> findEngineerOrders1(String tel){
        return ordersDao.findEngineerOrders1(tel);
    }

    public List<Orders> findEngineerOrders2(String tel){
        QueryWrapper<Orders> wrapper=new QueryWrapper<>();
        wrapper.eq("engineer_tel",tel);
        wrapper.eq("state",2);
        return ordersDao.selectList(wrapper);
    }

    public List<Orders> findEngineerOrders3(String tel){
        QueryWrapper<Orders> wrapper=new QueryWrapper<>();
        wrapper.eq("engineer_tel",tel);
        wrapper.eq("state",3);
        return ordersDao.selectList(wrapper);
    }
    public List<Orders> findEngineerOrders4(String tel){
        QueryWrapper<Orders> wrapper=new QueryWrapper<>();
        wrapper.eq("engineer_tel",tel);
        wrapper.eq("state",4);
        return ordersDao.selectList(wrapper);
    }

    public List<OrderEngineerInformation> selectEngineersByOrderUuidAndState(String orderUuid){
        return ordersDao.selectEngineersByOrderUuidAndState(orderUuid);
    }
    public OrderEngineerInformation selectOrderEngineer(String orderUuid){
        return ordersDao.selectOrderEngineer(orderUuid);
    }
    public int UserOrderAddEngineer(String uuid, String engineerTel){
        QueryWrapper<OrderEngineer> wrapper=new QueryWrapper<>();
        wrapper.eq("order_uuid",uuid);
        wrapper.eq("engineer_tel",engineerTel);
        OrderEngineer orderEngineer = orderEngineerDao.selectOne(wrapper);
        String distance=orderEngineer.getDistance();
        BigDecimal money=orderEngineer.getMoney();
        QueryWrapper<OrderEngineer> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_uuid",uuid);
        orderEngineerDao.delete(queryWrapper);
        Orders orders=new Orders();
        UpdateWrapper<Orders> ordersUpdateWrapper=new UpdateWrapper<>();
        ordersUpdateWrapper.set("engineer_tel",engineerTel);
        ordersUpdateWrapper.set("distance",distance);
        ordersUpdateWrapper.set("money",money);
        ordersUpdateWrapper.set("state",2);
        ordersUpdateWrapper.eq("order_uuid",uuid);
        return ordersDao.update(orders,ordersUpdateWrapper);
    }
    public int OrderConfirm(String uuid){
        Orders orders=new Orders();
        UpdateWrapper<Orders> ordersUpdateWrapper=new UpdateWrapper<>();
        ordersUpdateWrapper.set("state",3);
        ordersUpdateWrapper.eq("order_uuid",uuid);
        return ordersDao.update(orders,ordersUpdateWrapper);
    }
    public int UserOrderConfirm(String uuid){
        Orders orders=new Orders();
        UpdateWrapper<Orders> ordersUpdateWrapper=new UpdateWrapper<>();
        ordersUpdateWrapper.set("state",4);
        ordersUpdateWrapper.eq("order_uuid",uuid);
        return ordersDao.update(orders,ordersUpdateWrapper);
    }
}
